import { ref } from 'vue'

export interface ProjectIdentity {
  name: string
  comment: string
}

export function useGetProjectIdentities(baseUrl: string, params: { projectId: number }) {
  const identities = ref<ProjectIdentity[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)

  async function fetchIdentities() {
    loading.value = true
    error.value = null
    try {
      const url = `${baseUrl}/api/project/identities?projectId=${params.projectId}`
      const res = await fetch(url)
      if (!res.ok) throw new Error('网络错误')
      identities.value = await res.json()
    } catch (e: any) {
      error.value = e.message || '未知错误'
    } finally {
      loading.value = false
    }
  }

  return { identities, loading, error, fetchIdentities }
}
